package cc.rengu.igas.route.core.service.trans;

import cc.rengu.igas.route.common.constant.SmartRouteParamConstant;
import cc.rengu.igas.route.common.dao.FinanceTxnRouteCustomMapper;
import cc.rengu.igas.route.common.dao.NonFinanceTxnRouteCustomMapper;
import cc.rengu.igas.route.common.dao.impl.FinanceTxnRouteCustomMapperImpl;
import cc.rengu.igas.route.common.dao.impl.NonFinanceTxnRouteCustomMapperImpl;
import cc.rengu.igas.route.common.enums.RouteTxnNumEnum;
import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.igas.route.core.helper.RouterHelper;
import cc.rengu.igas.route.core.realize.credit.DstChannelCreditService;
import cc.rengu.igas.route.core.realize.payment.DstChannelPaymentService;
import cc.rengu.igas.route.core.realize.payment.DstChannelPaymentSignService;
import cc.rengu.igas.route.facade.bean.PayeeInfo;
import cc.rengu.igas.route.facade.bean.PayerInfo;
import cc.rengu.igas.route.facade.request.IntelligenceResultQueryRequest;
import cc.rengu.igas.route.facade.response.IntelligenceResultQueryResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.MchntOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.MchntOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.PayOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.*;
import cc.rengu.oltp.service.common.enums.BizStatusEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.OrderStatusEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.RespCodeService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.service.realize.impl.RespCodeServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 结果查询
 *
 * @author xionglz
 * @version 1.0
 * @date 2020-04-28
 */
public class IntelligenceResultQueryService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {
            IntelligenceResultQueryRequest resultQueryRequest = new IntelligenceResultQueryRequest();
            ConvertUtil.convertOutput(resultQueryRequest);
            if (cc.rengu.oltp.utility.util.StringUtil.isEmptyOrNull(resultQueryRequest.getFrontChannelId())) {
                rglog.error("必须上送前置渠道标志[frontChannelId]");
                throw new BizException(SmartRouteRspEnums.MSG_VALID_CHECK_ERROR.getRespCode(), SmartRouteRspEnums.MSG_VALID_CHECK_ERROR.getRespDesc());
            }

            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.SRC_SYS_ID, resultQueryRequest.getHeader().getSrcSysId());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM, resultQueryRequest.getHeader().getTraceNo());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TRANS_TIME, resultQueryRequest.getHeader().getTransTime());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TRANS_DATE, resultQueryRequest.getHeader().getTransDate());

            IntelligenceResultQueryResponse queryResponse = new IntelligenceResultQueryResponse();
            queryResponse.setHeader(resultQueryRequest.getHeader());
            //处理附加信息
            if (!cc.rengu.oltp.utility.util.StringUtil.isEmptyOrNull(resultQueryRequest.getAdditionInfo())) {
                JSONObject jsonObject = JSONObject.parseObject(resultQueryRequest.getAdditionInfo());
                jsonObject.forEach((key, value) -> {
                    try {
                        xmlTreeUtil.setXmlTreeStringValue(key, (String) value);
                    } catch (Exception e) {
                        rglog.error(e.getMessage());
                    }
                });
            }
            String dstChannelId, origSysSeqNum;
            //查询非金融流水表
            if (RouteTxnNumEnum.PAYMENT_SIGN.getTxnNum().equalsIgnoreCase(resultQueryRequest.getOrigTxnNum())) {
                //查询非金融交易流水表获取签约时的通道
                rglog.debug("查询非金融交易流水表获取签约时的通道");
                NonFinanceTxnRouteCustomMapper nonFinanceTxnMapper = new NonFinanceTxnRouteCustomMapperImpl();
                List<NonFinanceTxn> nonFinanceTxns = nonFinanceTxnMapper.fetchTxnByFrontSeqNumWithNoDateTime(
                        resultQueryRequest.getHeader().getInstId(), resultQueryRequest.getOrigTxnDate(),
                        resultQueryRequest.getOrigOrder(), resultQueryRequest.getHeader().getSrcSysId(), resultQueryRequest.getHeader().getChanlId());
                if (nonFinanceTxns == null) {
                    xmlTreeUtil.deleteXmlTreeNode("status");
                    xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespCode());
                    xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespDesc());
                    queryResponse.setRespCode(SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespCode());
                    queryResponse.setRespDesc(SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespDesc());
                    ConvertUtil.convertInput(queryResponse);
                    rglog.error("根据原交易订单号<origOrderId:{}>：未查询到流水,非查询数据库失败，认为查询交易成功，查询结果为原交易失败", resultQueryRequest.getOrigOrder());
                    return 0;
                } else if (nonFinanceTxns.size() != 1) {
                    xmlTreeUtil.deleteXmlTreeNode("status");
                    rglog.error("根据原交易订单号：<origOrderId:{}>查询到多条", resultQueryRequest.getOrigOrder());
                    throw new BizException(SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespCode(), SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespDesc());
                }
                NonFinanceTxn nonFinanceTxn = nonFinanceTxns.get(0);
                dstChannelId = nonFinanceTxn.getDevBrand();
                resultQueryRequest.setOrigTxnTime(nonFinanceTxn.getTransDateTime().substring(9));
                if (!StringUtil.isNullorEmpty(nonFinanceTxn.getRemark4())) {
                    resultQueryRequest.setAdditionInfo(nonFinanceTxn.getRemark4());
                }
                origSysSeqNum = nonFinanceTxn.getSysSeqNum();
                xmlTreeUtil.setXmlTreeStringValue("origSysSeqNum", origSysSeqNum);
            } else {
                //查询金融交易流水表获取原通道
                rglog.debug("查询金融交易流水表获取原通道");
                FinanceTxnRouteCustomMapper financeTxnMapper = new FinanceTxnRouteCustomMapperImpl();
                List<FinanceTxn> financeTxns = financeTxnMapper.fetchTxnByFrontSeqNumWithNoDateTime(
                        resultQueryRequest.getHeader().getInstId(), resultQueryRequest.getOrigTxnDate(),
                        resultQueryRequest.getOrigOrder(), resultQueryRequest.getHeader().getSrcSysId(), resultQueryRequest.getHeader().getChanlId());
                if (financeTxns == null) {
                    xmlTreeUtil.deleteXmlTreeNode("status");
                    //可能存在查询后发先至的问题，不能认为原交易为失败交易，若超过一定时间（一般为5-10分钟）查询不到交易则原交易一定为失败
                    rglog.error("根据原交易订单号<origOrderId:{}>：未查询到流水,非查询数据库失败，认为查询交易失败返回未查询到原交易", resultQueryRequest.getOrigOrder());
                    throw new BizException(SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespCode(), SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespDesc());
                } else if (financeTxns.size() != 1) {
                    xmlTreeUtil.deleteXmlTreeNode("status");
                    rglog.error("根据原交易订单号：<origOrderId:{}>查询到多条", resultQueryRequest.getOrigOrder());
                    throw new BizException(SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespCode(), SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespDesc());
                }
                FinanceTxn financeTxn = financeTxns.get(0);
                dstChannelId = financeTxn.getDevBrand();
                resultQueryRequest.setOrigTxnTime(financeTxn.getTransDateTime().substring(8));
                if (!StringUtil.isNullorEmpty(financeTxn.getRemark4())) {
                    resultQueryRequest.setAdditionInfo(financeTxn.getRemark4());
                }
                origSysSeqNum = financeTxn.getSysSeqNum();
                xmlTreeUtil.setXmlTreeStringValue("origSysSeqNum", origSysSeqNum);
            }
            MchntOrderInfo mchntOrderInfo;
            PayOrderInfo payOrderInfo;
            UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
            UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(resultQueryRequest.getHeader().getInstId(), origSysSeqNum);
            if (null == userOrderInfo) {
                xmlTreeUtil.deleteXmlTreeNode("status");
                //此时未查询到订单号还可以查询，根据查询结果插入订单或者不处理
                rglog.warn("根据原交易订单号：<origOrderId:{}>未查询到订单，可能查询交易后发先至", resultQueryRequest.getOrigOrder());
                queryResponse = new IntelligenceResultQueryResponse();
                queryResponse.setHeader(resultQueryRequest.getHeader());
                queryResponse.setRespCode(SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespCode());
                queryResponse.setRespDesc(SmartRouteRspEnums.NO_ORIG_TRANS_ERROR.getRespDesc());
                ConvertUtil.convertInput(queryResponse);
                return 0;
            } else {
                MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
                mchntOrderInfo = mchntOrderInfoMapper.selectMchntOrderInfoByPrimaryKey(resultQueryRequest.getHeader().getInstId(), origSysSeqNum, resultQueryRequest.getMchntNo());
                payOrderInfo = new PayOrderInfoMapperImpl().selectPayOrderInfoByPrimaryKey(resultQueryRequest.getHeader().getInstId(), origSysSeqNum, dstChannelId, "0");
                xmlTreeUtil.setXmlTreeObjectValue("ORIG_PAY_ORDER_INFO", payOrderInfo);
            }
            //若果有最终的结果成功，失败则直接应答
            if (BizStatusEnum.FINISH.getStatus().equals(payOrderInfo.getBizStatus())) {
                queryResponse.setHeader(resultQueryRequest.getHeader());
                //设置返回值
                queryResponse.setDstChannelId(dstChannelId);
                queryResponse.setMchntNo(payOrderInfo.getMchntNo());
                queryResponse.setSettleDate(payOrderInfo.getSettleDate());
                queryResponse.setDstChannelSsn(payOrderInfo.getChannelSeqNum());
                queryResponse.setTxnAmt(payOrderInfo.getOrderAmt());
                queryResponse.setTxnCrcyCode(payOrderInfo.getOrderCcy());
                queryResponse.setBizCode(payOrderInfo.getRemark1());
                queryResponse.setOrigOrder(payOrderInfo.getMchntOrderId());
                queryResponse.setOrigTxnDate(payOrderInfo.getTxnDate());
                queryResponse.setOrigTxnTime(payOrderInfo.getTxnTime());
                if (RouteTxnNumEnum.CREDIT.getTxnNum().equalsIgnoreCase(resultQueryRequest.getOrigTxnNum())) {
                    PayeeInfo payeeInfo = new PayeeInfo();
                    payeeInfo.setAcctNo(payOrderInfo.getPayeeAcctInfo());
                    payeeInfo.setAcctType(payOrderInfo.getPayeeAcctType());
                    queryResponse.setPayeeInfo(payeeInfo);
                    queryResponse.setPostscript(payOrderInfo.getPayeeRemark());
                }
                if (RouteTxnNumEnum.PAYMENT.getTxnNum().equalsIgnoreCase(resultQueryRequest.getOrigTxnNum())) {
                    PayerInfo payerInfo = new PayerInfo();
                    payerInfo.setAcctNo(payOrderInfo.getPayeeAcctInfo());
                    payerInfo.setAcctType(payOrderInfo.getPayeeAcctType());
                    queryResponse.setPayerInfo(payerInfo);
                    queryResponse.setPostscript(payOrderInfo.getPayerRemark());
                }
                queryResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                queryResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                queryResponse.setOrigRetCode(payOrderInfo.getRespCode());
                queryResponse.setOrigRetMsg(payOrderInfo.getRespDesc());
                xmlTreeUtil.deleteXmlTreeNode("status");
                if (TransStatusEnum.TRANS_SUCC.getStatus().equals(payOrderInfo.getTransStatus())) {
                    queryResponse.setStatus("0");
                } else if (TransStatusEnum.SYSTEM_REJECT.getStatus().equals(payOrderInfo.getTransStatus()) /*||
                        TransStatusEnum.SYSTEM_REJECT.getStatus().equals(payOrderInfo.getTransStatus())*/) {
                    queryResponse.setStatus("1");
                } else {
                    queryResponse.setStatus("2");
                }
                ConvertUtil.convertInput(queryResponse);
                return 0;
            }
            //根据通道标识，通道交易码查询参数表
            SysParamService sysParamService = new SysParamServiceImpl();
            String paramType = SmartRouteParamConstant.PARAM_TYPE_PREFIX_CHANNEL_INSTANCE + dstChannelId;
            SysParam sysParam = sysParamService.getSysParamInfo(resultQueryRequest.getHeader().getInstId(), paramType, resultQueryRequest.getOrigTxnNum());
            if (sysParam == null || cc.rengu.oltp.utility.util.StringUtil.isEmptyOrNull(sysParam.getParamValue()) || !SmartRouteParamConstant.YES.equalsIgnoreCase(sysParam.getParamStatus())) {
                xmlTreeUtil.deleteXmlTreeNode("status");
                rglog.error("根据通道标识<channelId:{}>，通道交易码<channelTxnId:{}>未找到当前法人<{}>或公共实现类", paramType, resultQueryRequest.getOrigTxnNum(), resultQueryRequest.getHeader().getInstId());
                throw new BizException(SmartRouteRspEnums.CONFIG_ERROR.getRespCode(), SmartRouteRspEnums.CONFIG_ERROR.getRespDesc());
            }

            Class c = Class.forName(sysParam.getParamValue());

            //通道压力+1
            boolean isIncrSucc = true;
            String keyStress = SmartRouteParamConstant.ROUTE_DATA_ALL_STRESS + ":" + AppParamConstant.DEFAULT_INSTID + ":" + dstChannelId;
            try {
                RedisUtil.onceIncr(keyStress);
            } catch (Exception e) {
                rglog.warn(cc.rengu.utility.base.StringUtil.ExceptionToString(e));
                isIncrSucc = false;
            }

            try {
                if (RouteTxnNumEnum.PAYMENT.getTxnNum().equalsIgnoreCase(resultQueryRequest.getOrigTxnNum())) {
                    DstChannelPaymentService paymentService = (DstChannelPaymentService) c.newInstance();
                    rglog.debug("调用通道代收查询");
                    //调用通道代收查询
                    queryResponse = paymentService.query(resultQueryRequest);
                } else if (RouteTxnNumEnum.CREDIT.getTxnNum().equalsIgnoreCase(resultQueryRequest.getOrigTxnNum())) {
                    DstChannelCreditService creditService = (DstChannelCreditService) c.newInstance();
                    //调用通道代付查询
                    rglog.debug("调用通道代付查询");
                    queryResponse = creditService.query(resultQueryRequest);

                } else {
                    DstChannelPaymentSignService paymentSignService = (DstChannelPaymentSignService) c.newInstance();
                    //调用通道签约查询
                    queryResponse = paymentSignService.query(resultQueryRequest);
                }
            } finally {
                //通道调用成功，redis计数器自减1(通道压力减少1)
                try {
                    if (isIncrSucc) {
                        RedisUtil.onceDecr(keyStress);
                    }
                } catch (Exception e) {
                    rglog.warn("通道<{}>压力统计redis计数器压力值-1异常{}", dstChannelId, cc.rengu.utility.base.StringUtil.ExceptionToString(e));
                }
            }

            if (null != queryResponse && !StringUtil.isNullorEmpty(queryResponse.getOrigRetCode())) {
                //设置返回值
                queryResponse.setHeader(resultQueryRequest.getHeader());
                queryResponse.setOrigOrder(payOrderInfo.getMchntOrderId());
                queryResponse.setOrigTxnDate(payOrderInfo.getTxnDate());
                queryResponse.setOrigTxnTime(payOrderInfo.getTxnTime());
                queryResponse.setDstChannelId(dstChannelId);
                queryResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                queryResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                queryResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                //代收和代付更新订单状态
                if (RouteTxnNumEnum.PAYMENT.getTxnNum().equalsIgnoreCase(resultQueryRequest.getOrigTxnNum()) ||
                        RouteTxnNumEnum.CREDIT.getTxnNum().equalsIgnoreCase(resultQueryRequest.getOrigTxnNum())) {
                    //转换应答码状态
                    RespCodeService respCodeService = new RespCodeServiceImpl();
                    BizResponse<IntelligenceResultQueryResponse> response = new BizResponse<>();
                    response.setRespCode(queryResponse.getOrigRetCode());
                    response.setRespDesc(queryResponse.getOrigRetMsg());
                    response.setRspSysId(dstChannelId);
                    response.setResult(queryResponse);
                    TransStatusEnum transStatusEnum = respCodeService.transferRespCode(response.getRspSysId(), response.getRespCode(), response.getRespDesc());
                    response.setTransStatus(transStatusEnum);
                    xmlTreeUtil.deleteXmlTreeNode("status");
                    if (TransStatusEnum.TRANS_SUCC.getStatus().equals(response.getTransStatus().getStatus())) {
                        //成功
                        queryResponse.setStatus("0");
                        rglog.info("交易成功");
                    } else if (TransStatusEnum.TRANS_INIT.getStatus().equals(response.getTransStatus().getStatus()) ||
                            TransStatusEnum.TRANS_TIMEOUT.getStatus().equals(response.getTransStatus().getStatus())) {
                        //未知
                        queryResponse.setStatus("2");
                        rglog.info("交易未知");
                    } else {
                        //失败
                        queryResponse.setStatus("1");
                        rglog.info("交易失败");
                    }
                    ConvertUtil.convertInput(queryResponse);
                    rglog.info("交易状态:<{}>", xmlTreeUtil.getXmlTreeStringValue("status"));
                    //非初始/未知状态则更新订单，否则不更新订单
                    if (!TransStatusEnum.TRANS_INIT.getStatus().equals(response.getTransStatus().getStatus()) &&
                            !TransStatusEnum.TRANS_TIMEOUT.getStatus().equals(response.getTransStatus().getStatus())) {
                        String dateTime23 = DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS");
                        OrderServiceImpl orderService = new OrderServiceImpl();
                        if (!orderService.updateOrderInfo(updUserOrderInfo(userOrderInfo, response, dateTime23), updMchntOrderInfo(mchntOrderInfo, response, dateTime23), updPayOrderInfo(payOrderInfo, response, dateTime23))) {
                            rglog.error("智能路由更新订单失败记录应答信息到日志:");
                            rglog.error(JSON.toJSONString(response));
                        }
                        if (RouteTxnNumEnum.PAYMENT.getTxnNum().equalsIgnoreCase(resultQueryRequest.getOrigTxnNum())) {
                            //代收只统计明确成功
                            if (TransStatusEnum.TRANS_SUCC.getStatus().equals(transStatusEnum.getStatus())) {
                                RouterHelper.statisAmtLimit(resultQueryRequest.getHeader().getInstId(), payOrderInfo.getTxnNum(), payOrderInfo.getDstTxnNum(), dstChannelId,
                                        StringUtil.isNullorEmpty(null == queryResponse.getPayerInfo() ? null : queryResponse.getPayerInfo().getAcctType()) ? payOrderInfo.getPayerAcctType() : queryResponse.getPayerInfo().getAcctType(), payOrderInfo.getOrderAmt(), rglog);
                                String keySuccCount = SmartRouteParamConstant.ROUTE_DATA_ALL_SUCC_COUNT + ":" + AppParamConstant.DEFAULT_INSTID + ":" + dstChannelId;
                                try {
                                    RedisUtil.onceIncr(keySuccCount);
                                } catch (Exception e) {
                                    rglog.warn("通道<{}>成功交易总比数统计redis计数器+1异常{}", dstChannelId, cc.rengu.utility.base.StringUtil.ExceptionToString(e));
                                }
                            }
                        } else {
                            //代付只统计明确成功
                            rglog.trace("代付：payOrderInfo = <{}>", JSONObject.toJSONString(payOrderInfo));
                            rglog.trace("代付：queryResponse = <{}>", JSONObject.toJSONString(queryResponse));
                            if (TransStatusEnum.TRANS_SUCC.getStatus().equals(transStatusEnum.getStatus())) {
                                RouterHelper.statisAmtLimit(resultQueryRequest.getHeader().getInstId(), payOrderInfo.getTxnNum(), payOrderInfo.getDstTxnNum(), dstChannelId,
                                        StringUtil.isNullorEmpty(null == queryResponse.getPayeeInfo() ? null : queryResponse.getPayeeInfo().getAcctType()) ? payOrderInfo.getPayeeAcctType() : queryResponse.getPayeeInfo().getAcctType(), payOrderInfo.getOrderAmt(), rglog);

                                String keySuccCount = SmartRouteParamConstant.ROUTE_DATA_ALL_SUCC_COUNT + ":" + AppParamConstant.DEFAULT_INSTID + ":" + dstChannelId;
                                try {
                                    RedisUtil.onceIncr(keySuccCount);
                                } catch (Exception e) {
                                    rglog.warn("通道<{}>成功交易总比数统计redis计数器+1异常{}", dstChannelId, cc.rengu.utility.base.StringUtil.ExceptionToString(e));
                                }
                            }
                        }
                    }
                }
            } else {
                xmlTreeUtil.deleteXmlTreeNode("status");
                queryResponse = new IntelligenceResultQueryResponse();
                queryResponse.setHeader(resultQueryRequest.getHeader());
                queryResponse.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                queryResponse.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
                ConvertUtil.convertInput(queryResponse);
            }
        } catch (BizException e) {
            try {
                xmlTreeUtil.deleteXmlTreeNode("status");
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(cc.rengu.utility.base.StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(cc.rengu.utility.base.StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.deleteXmlTreeNode("status");
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            } catch (Exception e1) {
                rglog.error(cc.rengu.utility.base.StringUtil.ExceptionToString(e1));
            }
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

    private UserOrderInfo updUserOrderInfo(UserOrderInfo origUserOrderInfo, BizResponse<IntelligenceResultQueryResponse> response, String dateTime23) {
        if (TransStatusEnum.TRANS_SUCC.getStatus().equals(response.getTransStatus().getStatus())) {
            origUserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
            origUserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
        } else if (TransStatusEnum.TRANS_TIMEOUT.getStatus().equals(response.getTransStatus().getStatus())) {
            origUserOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
            origUserOrderInfo.setOrderStatus(OrderStatusEnum.INIT.getStatus());
        } else if (TransStatusEnum.SYSTEM_REJECT.getStatus().equals(response.getTransStatus().getStatus())) {
            origUserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            origUserOrderInfo.setOrderStatus(OrderStatusEnum.CLOSE.getStatus());
        }/*else if(TransStatusEnum.SYSTEM_REJECT.getStatus().equals(response.getTransStatus().getStatus()) ){
            origUserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            origUserOrderInfo.setOrderStatus(OrderStatusEnum.CLOSE.getStatus());
        }*/ else {
            origUserOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
            origUserOrderInfo.setOrderStatus(OrderStatusEnum.INIT.getStatus());
        }
        origUserOrderInfo.setTransStatus(response.getTransStatus().getStatus());
        origUserOrderInfo.setUpdateTime(dateTime23);
        return origUserOrderInfo;
    }

    private List<MchntOrderInfo> updMchntOrderInfo(MchntOrderInfo origMchntOrderInfo, BizResponse<IntelligenceResultQueryResponse> response, String dateTime23) {
        List<MchntOrderInfo> mchntOrderInfos = new ArrayList<>();
        mchntOrderInfos.add(origMchntOrderInfo);
        if (TransStatusEnum.TRANS_SUCC.getStatus().equals(response.getTransStatus().getStatus())) {
            origMchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
        } else if (TransStatusEnum.TRANS_TIMEOUT.getStatus().equals(response.getTransStatus().getStatus())) {
            origMchntOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        } else if (TransStatusEnum.SYSTEM_REJECT.getStatus().equals(response.getTransStatus().getStatus())) {
            origMchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
        }/*else if(TransStatusEnum.SYSTEM_REJECT.getStatus().equals(response.getTransStatus().getStatus()) ){
            origMchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
        }*/ else {
            origMchntOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        }
        origMchntOrderInfo.setTransStatus(response.getTransStatus().getStatus());
        origMchntOrderInfo.setUpdateTime(dateTime23);
        return mchntOrderInfos;
    }

    private PayOrderInfo updPayOrderInfo(PayOrderInfo origPayOrderInfo, BizResponse<IntelligenceResultQueryResponse> response, String dateTime23) {
        if (TransStatusEnum.TRANS_SUCC.getStatus().equals(response.getTransStatus().getStatus())) {
            origPayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
        } else if (TransStatusEnum.TRANS_TIMEOUT.getStatus().equals(response.getTransStatus().getStatus())) {
            origPayOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        } else if (TransStatusEnum.SYSTEM_REJECT.getStatus().equals(response.getTransStatus().getStatus())) {
            origPayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
        }/*else if(TransStatusEnum.SYSTEM_REJECT.getStatus().equals(response.getTransStatus().getStatus()) ){
            origPayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
        }*/ else {
            origPayOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        }
        origPayOrderInfo.setTransStatus(response.getTransStatus().getStatus());
        if (!StringUtil.isNullorEmpty(response.getResult().getSettleDate())) {
            origPayOrderInfo.setSettleDate(response.getResult().getSettleDate());
        }
        if (!StringUtil.isNullorEmpty(response.getResult().getDstChannelSsn())) {
            origPayOrderInfo.setChannelSeqNum(response.getResult().getDstChannelSsn());
        }
        origPayOrderInfo.setUpdateTime(dateTime23);
        return origPayOrderInfo;
    }
}
